package sige

import org.springframework.dao.DataIntegrityViolationException

class SecretariaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        def lista = []
        params.max = Math.min(max ?: 10, 100)
        if( params.campo ){
            lista = Secretaria.findAllByNomeIlike("%${params.campo}%",
                [ max: params.max, sort: params.sort, order: params.order, offset: params.offset ])
        }else{
            lista = Professor.list(params)
        }
        [secretariaInstanceList: lista, secretariaInstanceTotal: lista.size()]
    }

    def create() {
        [secretariaInstance: new Secretaria(params)]
    }

    def save() {
        def secretariaInstance = new Secretaria(params)
        if (!secretariaInstance.save(flush: true)) {
            render(view: "create", model: [secretariaInstance: secretariaInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), secretariaInstance.id])
        redirect(action: "show", id: secretariaInstance.id)
    }

    def show(Long id) {
        def secretariaInstance = Secretaria.get(id)
        if (!secretariaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), id])
            redirect(action: "list")
            return
        }

        [secretariaInstance: secretariaInstance]
    }

    def edit(Long id) {
        def secretariaInstance = Secretaria.get(id)
        if (!secretariaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), id])
            redirect(action: "list")
            return
        }

        [secretariaInstance: secretariaInstance]
    }

    def update(Long id, Long version) {
        def secretariaInstance = Secretaria.get(id)
        if (!secretariaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (secretariaInstance.version > version) {
                secretariaInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'secretaria.label', default: 'Secretaria')] as Object[],
                          "Another user has updated this Secretaria while you were editing")
                render(view: "edit", model: [secretariaInstance: secretariaInstance])
                return
            }
        }

        secretariaInstance.properties = params

        if (!secretariaInstance.save(flush: true)) {
            render(view: "edit", model: [secretariaInstance: secretariaInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), secretariaInstance.id])
        redirect(action: "show", id: secretariaInstance.id)
    }

    def delete(Long id) {
        def secretariaInstance = Secretaria.get(id)
        if (!secretariaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), id])
            redirect(action: "list")
            return
        }

        try {
            secretariaInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'secretaria.label', default: 'Secretaria'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def updateTable = {
        def lista = Secretaria.findAllByNomeIlike("%${params.value}%")
        
        render (template: 'table', model:[secretariaInstanceList: lista, 
            secretariaInstanceTotal: lista.size()])
    }
}
